﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Input;
using Jerry.DroidBuilder.Controls;

namespace Jerry.DroidBuilder.Extensions
{
    public static class ColumnExtension
    {
        #region IsEnabled Property

        /// <summary>
        /// Gets the width of the column.
        /// </summary>
        public static bool GetIsEnabled(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsEnabledProperty);
        }

        /// <summary>
        /// Sets the width of the column.
        /// </summary>
        public static void SetIsEnabled(DependencyObject obj, bool isEnabled)
        {
            obj.SetValue(IsEnabledProperty, isEnabled);
        }

        /// <summary>
        /// Identifies the <see cref="Width"/> dependency property.
        /// </summary>
        public static readonly DependencyProperty IsEnabledProperty =
            DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(ColumnExtension), new FrameworkPropertyMetadata(OnIsEnabledChanged));

        private static void OnIsEnabledChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            TreeListView listView = sender as TreeListView;

            if (null != listView && (bool)e.NewValue)
            {
                ColumnExtensionHelper columnBehaviorResizeHelper = new ColumnExtensionHelper(listView);
            }
        }

        #endregion

        #region Width Property

        /// <summary>
        /// Gets the width of the column.
        /// </summary>
        public static GridLength GetWidth(DependencyObject obj)
        {
            return (GridLength)obj.GetValue(WidthProperty);
        }

        /// <summary>
        /// Sets the width of the column.
        /// </summary>
        public static void SetWidth(DependencyObject obj, GridLength width)
        {
            obj.SetValue(WidthProperty, width);
        }

        /// <summary>
        /// Identifies the <see cref="Width"/> dependency property.
        /// </summary>
        public static readonly DependencyProperty WidthProperty =
            DependencyProperty.RegisterAttached("Width", typeof(GridLength), typeof(ColumnExtension), new FrameworkPropertyMetadata());

        #endregion

        #region MinWidth Property

        /// <summary>
        /// Gets the minimum width of the column.
        /// </summary>
        public static double GetMinWidth(DependencyObject obj)
        {
            return (double)obj.GetValue(MinWidthProperty);
        }

        /// <summary>
        /// Sets the minimum width of the column.
        /// </summary>
        public static void SetMinWidth(DependencyObject obj, double minWidth)
        {
            obj.SetValue(MinWidthProperty, minWidth);
        }

        /// <summary>
        /// Identifies the <see cref="MinWidth"/> dependency property.
        /// </summary>
        public static readonly DependencyProperty MinWidthProperty =
            DependencyProperty.RegisterAttached("MinWidth", typeof(double), typeof(ColumnExtension), new FrameworkPropertyMetadata(30d));

        #endregion

        public static bool IsProportionalColumn(GridViewColumn gridViewColumn)
        {
            return GetWidth(gridViewColumn).IsStar;
        }
    }
}
